package com.feishi.core.util.queue.feishi;


import com.feishi.core.util.queue.Customer;

import java.util.Queue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * Created by caixq on 4/26 026.
 */
public class FeishiExecutor<T,R> implements Runnable{
    Queue<Msg<T,R>> queue=new LinkedBlockingQueue<>();
    Customer customer;
    /**
     * 0:未执行
     * 1:执行中
     * 2:执行结束
     * */
    int execStatus=0;
    public FeishiExecutor() {

    }

    @Override
    public void run() {
        execStatus=1;
        int index=0;
        while (!queue.isEmpty()){
            Msg<T, R> msg = queue.poll();
            try {
                customer.execute(msg,index++,queue.isEmpty());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        execStatus=0;

    }
    public FeishiExecutor(Customer customer) {
        this.customer = customer;
    }
    public void add(Msg<T,R> msg){
        queue.add(msg);
    }

}
